/**
 * -----------------------------------
 *  Copyright (c) 2021-2025
 *  All rights reserved, Designed By www.linfengtech.cn
 *  林风社交论坛商业版本请务必保留此注释头信息
 *  商业版授权联系技术客服	 QQ:  3582996245
 *  严禁分享、盗用、转卖源码或非法牟利！
 *  版权所有 ，侵权必究！
 * -----------------------------------
 */
package io.linfeng.modules.admin.controller;

import java.util.Arrays;
import java.util.Map;

import io.linfeng.common.annotation.NoRepeatSubmit;
import io.linfeng.common.annotation.SysLog;
import io.linfeng.common.validator.ValidatorUtils;
import io.linfeng.modules.app.param.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.linfeng.modules.admin.entity.PostEntity;
import io.linfeng.modules.admin.service.PostService;
import io.linfeng.common.utils.PageUtils;
import io.linfeng.common.utils.R;



/**
 * 
 * 帖子管理
 * @author linfeng
 * @email 2445465217@qq.com
 * @date 2022-01-23 20:49:55
 */
@RestController
@RequestMapping("admin/post")
@Api(tags = "管理端——帖子管理")
public class PostController {
    @Autowired
    private PostService postService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @RequiresPermissions("admin:post:list")
    @ApiOperation("帖子列表")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = postService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @RequiresPermissions("admin:post:info")
    @ApiOperation("帖子详情")
    public R info(@PathVariable("id") Integer id){
		PostEntity post = postService.getById(id);

        return R.ok().put("post", post);
    }



    @PostMapping("/save")
    @RequiresPermissions("admin:post:save")
    @ApiOperation("帖子保存")
    @NoRepeatSubmit
    public R save(@RequestBody PostEntity post){
		postService.save(post);

        return R.ok();
    }



    @SysLog("修改帖子")
    @PostMapping("/update")
    @RequiresPermissions("admin:post:update")
    @ApiOperation("修改帖子")
    @NoRepeatSubmit
    public R update(@RequestBody PostEntity post){
		postService.updateById(post);

        return R.ok();
    }



    @SysLog("批量删除帖子")
    @PostMapping("/delete")
    @RequiresPermissions("admin:post:delete")
    @ApiOperation("批量删除帖子")
    @NoRepeatSubmit
    public R delete(@RequestBody Integer[] ids){
        postService.deleteByAdmin(Arrays.asList(ids));
        return R.ok();
    }

    @SysLog("删除帖子")
    @PostMapping("/deleteById")
    @RequiresPermissions("admin:post:delete")
    @ApiOperation("删除帖子")
    @NoRepeatSubmit
    public R deleteById(@RequestBody DeletePostForm param){
        postService.deletePostIdByAdmin(param);
        return R.ok();
    }


    @SysLog("下架帖子")
    @PostMapping("/down")
    @RequiresPermissions("admin:post:update")
    @ApiOperation("下架帖子")
    @NoRepeatSubmit
    public R down(@RequestBody DownPostForm param){
        postService.downByAdmin(param);
        return R.ok();
    }

    @SysLog("上架帖子")
    @PostMapping("/up")
    @RequiresPermissions("admin:post:update")
    @ApiOperation("上架帖子")
    @NoRepeatSubmit
    public R up(@RequestBody Integer[] ids){
        postService.upByAdmin(Arrays.asList(ids));
        return R.ok();
    }

    @SysLog("批量执行视频贴上下架")
    @PostMapping("/doJob")
    @RequiresPermissions("admin:post:update")
    @ApiOperation("批量执行视频贴上下架")
    @NoRepeatSubmit
    public R doVideoPostJob(@RequestBody VideoPostForm param){
        postService.doVideoPostJob(param);
        return R.ok();
    }


    @PostMapping("/postSubmit")
    @RequiresPermissions("admin:post:save")
    @ApiOperation("发帖子")
    @NoRepeatSubmit
    public R postSubmit(@RequestBody AddPostByAdminForm request){
        ValidatorUtils.validateEntity(request);
        postService.postAddByAdmin(request);

        return R.ok();
    }
}
